const createParams=function (context,params){
    Object.keys(params).forEach(item=>{
        context[item]=params[item]
    })
}
//节流
export function throttle(fn,wait){
    var timer = null;
    return function(){
        var context = this;
        var args = arguments;
        if(!timer){
            timer = setTimeout(function(){
                fn.apply(context,args);
                timer = null;
            },wait)
        }
    }
}
const getCurrentTime = function(fields = 's',time=null) {
    const date =time? new Date(time):new Date();
    let str = '';
    let y = date.getFullYear().toString();
    let m = (date.getMonth() + 1).toString();
    m = m.length === 1 ? '0' + m : m
    let d = date.getDate().toString();
    d = d.length === 1 ? '0' + d : d
    let h = date.getHours().toString();
    h = h.length === 1 ? '0' + h : h
    let i = date.getMinutes().toString();
    i = i.length === 1 ? '0' + i : i
    let s = date.getSeconds().toString();
    s = s.length === 1 ? '0' + s : s
    if (fields === 'y') {
        str = y;
    }
    if (fields === 'm') {
        str = y + '-' + m
    }
    if (fields === 'd') {
        str = y + '-' + m + '-' + d
    }
    if (fields === 'h') {
        str = y + '-' + m + '-' + d + ' ' + h
    }
    if (fields === 'i') {
        str = y + '-' + m + '-' + d + ' ' + h + ':' + i
    }
    if (fields === 's') {
        str = y + '-' + m + '-' + d + ' ' + h + ':' + i + ':' + s
    }
    return str;
}
const utcToTime = function(utc,fields = 's') {
    const date = new Date(utc);
    let str = '';
    let y = date.getFullYear().toString();
    let m = (date.getMonth() + 1).toString();
    m = m.length === 1 ? '0' + m : m
    let d = date.getDate().toString();
    d = d.length === 1 ? '0' + d : d
    let h = date.getHours().toString();
    h = h.length === 1 ? '0' + h : h
    let i = date.getMinutes().toString();
    i = i.length === 1 ? '0' + i : i
    let s = date.getSeconds().toString();
    s = s.length === 1 ? '0' + s : s
    if (fields === 'y') {
        str = y;
    }
    if (fields === 'm') {
        str = y + '-' + m
    }
    if (fields === 'd') {
        str = y + '-' + m + '-' + d
    }
    if (fields === 'h') {
        str = y + '-' + m + '-' + d + ' ' + h
    }
    if (fields === 'i') {
        str = y + '-' + m + '-' + d + ' ' + h + ':' + i
    }
    if (fields === 's') {
        str = y + '-' + m + '-' + d + ' ' + h + ':' + i + ':' + s
    }
    return str;
}
//处理多选对象
const multipleChoiceChangeObj = function(targetName, value,vm, max = -1) {
    const data=vm[targetName]
    if(!data[value.id]){
        if (max == -1 || Object.keys(data).length < max) {
            vm.$set(vm[targetName],value.id,value)
        } else {
            this.$message.success('最多只能选择${max}个')
        }
    }else{
        vm.$delete(vm[targetName],value.id)
    }
}
//处理多选数组
const multipleChoiceChange = function(arr, value, max = -1) {
    if (!arr.includes(value)) {
        if (max == -1 || arr.length < max) {
            arr.push(value)
        } else {
            this.$message.success('最多只能选择${max}个')
        }
    } else {
        const index = arr.indexOf(value)
        arr.splice(index, 1)
    }
    return arr;
}
export function randomString(len) {
    len = len || 32;
    let $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
    let maxPos = $chars.length;
    let pwd = '';
    for (let i = 0; i < len; i++) {
        pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
    }
    return pwd;
}
export function getSalaryData(select,step=1000,num=80){
    let start =[];
    let end =[];
    for(let i=1;i<=num;i++){
        let text=i*step;
        start.push(text);
        if(select&&select+5>=i&&i>=(select+1)){
            end.push(text)
        }
    }
    if(select==num){
        end.push(select*step)
    }
    return [start,end];
}
export default {
    createParams,
    getCurrentTime,
    throttle,
    utcToTime,
    multipleChoiceChangeObj,
    multipleChoiceChange
}
